Linux Loadable Module
A Linux Loadable Module is a feature of the Linux Kernel that allows users to add features to the system at runtime without requireing a reboot. The term is a.k.a LKM, Linux Module, Loadable Module, Kernel Module, or simply Module. Overview A Linux Loadable Module is an ELF object file. This ELF file is given to the "insmod" system call. Each module designates two functions to be called at module loading (init) and removing (exit) respectively. In the init function the module performs any initialization it needs such as allocating memory, calling functions to register a device driver or a file system, or hook some kernel functions like system calls. The exit function perform the opposite of the init function and frees all allocated objects. Definitions Modules VS Applications User Modules VS Kernel Modules Architecture How-To Getting Started Step1: Setting Up The Environment First you need to install a Linux source tree. You can either obtain one from an online mirror or use the one from the application repository of your Linux distribution. If you are using YUM (Yellow Update Manager) try the following command, as root, to install all required module development files: yum -y install kernel-devel For the following steps, assume you have one under /usr/src/kernels/2.6.X/. Step2: Writing The Code A skleleton of a linux module // Filename: hello.c #include #include MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { // moule is now being loaded return 0; } static void hello_exit(void) { //module is now being removed } module_init(hello_init); module_exit(hello_exit); Step3: Compiling and Building To compile kernel modules in Linux 2.6 and later, you will use the kernel build process. The build process is a collection of scripts, objects, and make files that use your custom made make file to build your modules. Assume your module source file is called hello.c and include util1.c and util2.c Make file # Filename makefile located in the same directory as hello.c # If KERNELRELEASE is defined, we've been invoked from the # kernel build system and can use its language. ifneq ($(KERNELRELEASE),) obj-m := hello.o module-objs := util1.o util2.o # Otherwise we were called directly from the command # line; invoke the kernel build system. else KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif To use this file, cd to its directory (same directory as hello.c) and execute the following command make Note that the name of the make file must be exactly "makefile". The result of compilation, among many other files, is the module file hello.ko Step4: Loading and Unloading Load with this command (as root): insmod ./hello.ko Unload with this command (as root): rmmod hello Notice the missing path and file extension in the remove Printing and Viewing Message from Kernel Space Access The Calling Process (Current process) Dividing Module Responsibility (Module Stacking) Load-time Configuration (Module Parameters) 'Handling Errors During Initialization' Using The goto Statement Using The Cleanup Function Pitfalls Mechanism and Policy The double-underscore function prefix ( _ _ ) The Invalid Module Format Error (Version Dependency) Free Up Unused Initialization Memory Unnecessary Exit Code Prevent Allocating Exit Functions In Kernels That Disallow Unloading Module Loading Races